//           utility.cxx
//  gio febbraio 14 17:50:31 2013
//  Copyright  2013  Massimiliano Maniscalco
//  <massi.neptune@yahoo.com>
// utility.cxx
//
// Copyright (C) 2013 - Massimiliano Maniscalco
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "utility.h"

namespace massi_utilities
{
	Glib::ustring get_xml_friendly_string ( const Glib::ustring &str )
	{
		Glib::ustring correct = str;
		Glib::ustring::size_type pos = 0;
		Glib::ustring::size_type end = correct.size();
		try
		{
			while ( pos < end )
			{
				Glib::ustring::value_type crt = correct[pos];
				switch ( crt )
				{
					case '&':
						correct.replace ( pos, 1, Glib::ustring( "&amp;" ) );
						pos += 4;
						end = correct.size();
						break;
					case '<':
						correct.replace ( pos, 1, Glib::ustring( "&lt;" ) );
						pos += 3;
						end = correct.size();
						break;
					case '>':
						correct.replace ( pos, 1, Glib::ustring( "&gt;" ) );
						pos += 3;
						end = correct.size();
						break;
					case '\'':
						correct.replace ( pos, 1, Glib::ustring( "&apos;" ) );
						pos += 5;
						end = correct.size();
						break;
					case '\"':
						correct.replace ( pos, 1, Glib::ustring( "&quot;" ) );
						pos += 5;
						end = correct.size();
						break;
				}
				pos++;
			}
		}
		catch(const std::exception& ex)
		{
			std::cerr << "standard exception: " << ex.what() << std::endl;
			std::cerr << "get_xml_friendly_string:: " << str.raw() << std::endl;
		}
		return correct;
	}

	bool is_lunedi_pasquetta ( struct tm *data_ora )
	{
		if ( data_ora->tm_year >= 101 && data_ora->tm_year <= 200 )
		{
			int giorno[] = { 16, 1, 21, 12, 28, 17, 9, 24, 13, 5, 25, 9, 1, 21, 6, 28, 17, 2, 22, 13, 5, 18, 10, 1, 21, 6, 29, 17, 2, 22, 14, 29, 18, 10, 26, 14, 6, 26, 11, 2, 22, 7, 30, 18, 10, 26, 15, 6, 19, 11, 3, 22, 7, 30, 19, 3, 23, 15, 31, 19, 11, 27, 16, 7, 30, 12, 4, 23, 15, 31, 20, 11, 27, 16, 8, 20, 12, 4, 24, 8, 31, 20, 5, 27, 16, 1, 21, 12, 4, 17, 9, 31, 13, 5, 25, 16, 1, 21, 13, 29 };
			int mese[] = { 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2 };
			if ( data_ora->tm_mday == giorno[data_ora->tm_year - 101] && data_ora->tm_mon == mese[data_ora->tm_year - 101] )
				return true;
		}
		return false;
	}
}
